package com.yisu.auction.api.core.service.impl;

import com.yisu.auction.api.core.mapper.MemberMapper;
import com.yisu.auction.api.core.mapper.SecKillActivityMapper;
import com.yisu.auction.api.core.mapper.SecKillLogMapper;
import com.yisu.auction.api.core.service.BaseService;
import com.yisu.auction.api.core.service.ISecKillActivityService;
import com.yisu.auction.api.domain.request.activityPackage.SecKillActivityRequest;
import com.yisu.auction.api.domain.vo.activityPackageVos.SecKillVo;
import com.yisu.auction.ewf.common.enums.ResponseCode;
import com.yisu.auction.ewf.common.utils.DateUtil;
import com.yisu.auction.ewf.common.web.JsonResult;
import com.yisu.auction.ewf.entity.Member;
import com.yisu.auction.ewf.entity.SecKillActivity;
import com.yisu.auction.ewf.entity.SecKillLog;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * author: 赵杰
 * Business Description：秒杀业务实现
 * createTime：2017/8/24
 */
@Service
public class SecKillActivityServiceImpl extends BaseService implements ISecKillActivityService {

    @Resource
    private SecKillActivityMapper secKillActivityMapper;
    @Resource
    private SecKillLogMapper secKillLogMapper;
    @Resource
    private MemberMapper memberMapper;

    /**
     * author: 赵杰
     * Business Description：获取当日的秒杀
     *
     * @return
     */
    @Override
    public List<SecKillVo> getCurrentDaySecKillList() {
        return secKillActivityMapper.getCurrentDaySecKillList();
    }


    /**
     * author: 赵杰
     * Business Description：购买秒杀资格
     *
     * @param secKillActivityRequest
     * @return
     */
    @Override
    public JsonResult buyQualification(SecKillActivityRequest secKillActivityRequest) throws Exception {
        /**
         * 检测用户余额是否足够，不够提示充值
         * 余额足够，判断是否正处于秒杀资格购买时间
         * 成功购买资格
         */
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSSSSS");
        Member member = memberMapper.selectByPrimaryKey(Long.valueOf(secKillActivityRequest.getMemberId()));
        if (member.getWallet().intValue() < 0 && member.getPresentWallet().intValue() < 0) {
            return new JsonResult(ResponseCode.FAILURE.code, "余额不足，请充值！");
        }
        SecKillActivity secKillActivity = secKillActivityMapper.selectByPrimaryKey(secKillActivityRequest.getSecKillId());
        if (secKillActivity == null) {
            return new JsonResult(ResponseCode.FAILURE.code, "非法请求接口");
        }
        if (secKillActivity.getStartTime().getTime() - (new Date()).getTime() < 0 && (DateUtil.diffValue(secKillActivity.getStartTime(), new Date()) > 1800)) {
            return new JsonResult(ResponseCode.FAILURE.code, "目前时段不是秒杀资格购买时段！");
        }
        SecKillLog insertSecKill = new SecKillLog();
        insertSecKill.setSeckillId(secKillActivityRequest.getSecKillId());
        insertSecKill.setCreateTime(new Date());
        insertSecKill.setMemberId(Long.valueOf(secKillActivityRequest.getMemberId()));
        insertSecKill.setSeckillResult(0);
        if (secKillLogMapper.insertSelective(insertSecKill) > 0) {
            return new JsonResult(ResponseCode.SUCCESS.code, "购买资格成功，请及时参与秒差，过时视为资格放弃！");
        } else {
            return new JsonResult(ResponseCode.FAILURE.code, "购买资格失败！");
        }
    }

    /**
     * author: 赵杰
     * Business Description：秒杀
     *
     * @param secKillActivityRequest
     * @return
     */
    @Override
    public JsonResult secKillAction(SecKillActivityRequest secKillActivityRequest) throws InterruptedException {
        /**
         * 优先检测用户是否存在资格
         * 存在资格，真实用户购买进去后，立马追加虚拟用户；
         * 如果时间间隔小于1000MS；这样的话睡眠1S
         */
        //获取主体信息
        SecKillActivity secKillActivity = secKillActivityMapper.selectByPrimaryKey(secKillActivityRequest.getSecKillId());
        if (secKillActivity == null) {
            return new JsonResult(ResponseCode.FAILURE.code, "非法请求！");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSSSSS");
        long diffValue = Math.abs(secKillActivity.getStartTime().getTime() - (new Date().getTime()));
        if (secKillActivity.getStartTime().getTime() > (new Date().getTime()) && diffValue < 1000) {
            Thread.sleep(1000);
        }
        if (secKillActivity.getEndTime().getTime() - (new Date().getTime()) <= 0) {
            return new JsonResult(ResponseCode.FAILURE.code, "本期秒杀活动已结束！");
        }
        SecKillLog secKillLog = secKillLogMapper.selectSecKillLogByQuery(secKillActivityRequest);
        if (secKillLog == null) {
            return new JsonResult(ResponseCode.FAILURE.code, "你没有秒杀资格！");
        }
        //执行秒杀
        if (secKillLogMapper.secKillAction(secKillActivityRequest) > 0) {
            //用户秒杀成功，秒极追加虚拟用户秒杀
            long memberId = memberMapper.getVirtualMemberForSecKill(secKillActivityRequest);
            SecKillLog virtualSecKillLog = new SecKillLog();
            virtualSecKillLog.setCreateTime(new Date());
            virtualSecKillLog.setSeckillResult(1);
            virtualSecKillLog.setMemberId(memberId);
            virtualSecKillLog.setSeckillTime(new Date());
            virtualSecKillLog.setSeckillId(secKillActivityRequest.getSecKillId());
            if (secKillLogMapper.insertSelective(virtualSecKillLog) > 0) {
                return new JsonResult(ResponseCode.SUCCESS.code, "秒杀成功，请等待结果！");
            }
        }
        return new JsonResult(ResponseCode.SUCCESS.code, "已经参与秒杀或资格已经过期！");
    }


}
